//
//  Team9ExpertTalkCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/15.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertTalkCell.h"
#import "Team9Header.h"
#import "Team9JQHeader.h"
#import "Team9DesignVC.h"

@interface Team9ExpertTalkCell ()

@property(nonatomic, strong) Team9BaseLabel *titlelabel;
@property(nonatomic, strong) Team9BaseLabel *nickLabel;
@property(nonatomic, strong) UIImageView *faceiImageVew;
@property(nonatomic, strong) Team9BaseLabel *contentlabel;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews1;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews2;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews3;
@property(nonatomic, strong) NSMutableArray<UIImageView *> *starImageViews4;

@property(nonatomic, strong) UIImageView *leftImageView;
@property(nonatomic, strong) UIImageView *rightImageView;
@property(nonatomic, strong) UIImageView *midelImageView;

@property(nonatomic, strong) Team9BaseView *myView;

@end
@implementation Team9ExpertTalkCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView{
    
    self.nickLabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:_nickLabel];
    
    self.faceiImageVew = [UIImageView new];
    [self addSubview:_faceiImageVew];
    
    self.contentlabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:_contentlabel];
    
    
    self.starImageViews1 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews1 addObject:starImageView];
        [self.contentView addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews2 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews2 addObject:starImageView];
        [self.contentView addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews3 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews3 addObject:starImageView];
        [self.contentView addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    self.starImageViews4 = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews4 addObject:starImageView];
        [self.contentView addSubview:starImageView];
        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    
    self.titlelabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.contentlabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    self.faceiImageVew.layer.cornerRadius = 10 * WIDTH / 375;
    self.faceiImageVew.layer.masksToBounds = YES;
    
    self.myView = [[Team9BaseView alloc]initWithType:ViewTypeWhiteBlack];
//    self.myView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self.contentView addSubview:_myView];
    
    self.titlelabel = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:_titlelabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.leftImageView = [UIImageView new];
    [self.myView addSubview:_leftImageView];
    self.leftImageView.userInteractionEnabled = YES;
    self.midelImageView = [UIImageView new];
    [self.myView addSubview:_midelImageView];
    self.midelImageView.userInteractionEnabled = YES;
    self.rightImageView = [UIImageView new];
    [self.myView addSubview:_rightImageView];
    self.rightImageView.userInteractionEnabled = YES;
    [self.leftImageView addGestureRecognizer:tap];
    [self.rightImageView addGestureRecognizer:tap];
    [self.midelImageView addGestureRecognizer:tap];
    
    self.titlelabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    self.nickLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.contentlabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    
    [self p_contraints];
}
- (void)tapAction:(id)sender{
    Team9DesignVC *designVc =[Team9DesignVC new];
    designVc.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page2?col_id=%@&tab=photo", self.model.col_id];
    self.vc.hidesBottomBarWhenPushed = YES;
    [self.vc.navigationController pushViewController:designVc animated:YES];
}
- (void)p_contraints{
//    [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).offset(10);
//        make.centerX.equalTo(self);
//    }];
    [self.faceiImageVew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(10);
        make.width.height.equalTo(@(20 * WIDTH / 375));
    }];
    [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.faceiImageVew);
        make.left.equalTo(self.faceiImageVew.mas_right).offset(10);
    }];
    
    Team9BaseLabel *label1 = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:label1];
    
    Team9BaseLabel *label2 = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:label2];
    
    Team9BaseLabel *label3 = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:label3];
    
    Team9BaseLabel *label4 = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:label4];
    
    label1.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label2.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label3.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    label4.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    
    label1.text = @"服务态度";
    label2.text = @"施工质量";
    label3.text = @"按时完成";
    label4.text = @"报价相符";
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.faceiImageVew.mas_bottom).offset(10);
        make.left.equalTo(self.faceiImageVew);
    }];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(@(WIDTH / 3 * 2));
        make.top.equalTo(label1);
    }];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label1.mas_bottom).offset(10);
        make.left.equalTo(self.faceiImageVew);
    }];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label3);
        make.left.equalTo(label2);
    }];
    
    [self array:_starImageViews1 mas_contrainstsLeft:label1];
    [self array:_starImageViews2 mas_contrainstsLeft:label2];
    [self array:_starImageViews3 mas_contrainstsLeft:label3];
    [self array:_starImageViews4 mas_contrainstsLeft:label4];
    
    [self.myView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentlabel.mas_bottom).offset(10);
        make.right.equalTo(self.contentlabel);
        make.left.equalTo(self.contentlabel);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    self.titlelabel.numberOfLines = 0;
    [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myView).offset(10);
        make.right.equalTo(self.myView).offset(-10);
        make.left.equalTo(self.myView).offset(10);
    }];
    self.contentlabel.numberOfLines = 0;
    [self.contentlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label3.mas_bottom).offset(10);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
    }];
    
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlelabel.mas_bottom).offset(10);
        make.left.equalTo(self.myView).offset(10);
        make.height.width.equalTo(@(100 * WIDTH / 375));
    }];
    [self.midelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlelabel.mas_bottom).offset(10);
        make.centerX.equalTo(self.myView);
        make.height.width.equalTo(@(100 * WIDTH / 375));
    }];
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlelabel.mas_bottom).offset(10);
        make.right.equalTo(self.myView).offset(-10);
        make.height.width.equalTo(@(100 * WIDTH / 375));
    }];
}


- (void)setModel:(Team9WorkReviewsModel *)model{
    if (_model != model) {
        _model = model;
    }
//    NSLog(@"%g", self.model.rating1.floatValue);
    self.contentlabel.text = self.model.content;
    self.titlelabel.text = self.model.col_info;
    self.nickLabel.text = self.model.user_nick;
    [self array:_starImageViews1 makeRatings:self.model.rating1.floatValue];
    [self array:_starImageViews2 makeRatings:self.model.rating2.floatValue];
    [self array:_starImageViews3 makeRatings:self.model.rating3.floatValue];
    [self array:_starImageViews4 makeRatings:self.model.rating4.floatValue];
    
    [Team9Tool sd_imageView:self.faceiImageVew image:self.model.user_face placeholderImage:@"333"];
    if (_model.col_photos) {
        NSArray *array = @[_leftImageView, _midelImageView, _rightImageView];
        for (int i = 0; i < 3; i++) {
            UIImageView *imageView = array[i];
            if (self.model.col_photos.count > i) {
                [Team9Tool sd_imageView:imageView image:self.model.col_photos[i][@"pic"] placeholderImage:@"111"];
                [imageView setHidden:NO];
            }
        }
    } else {
        [self.leftImageView setHidden:YES];
        [self.midelImageView setHidden:YES];
        [self.rightImageView setHidden:YES];
    }
}

- (void)array:(NSArray<UIImageView *> *)array
mas_contrainstsLeft:(UIView *)view{
    UIImageView *lastStarView = nil;
    for (UIImageView *imageView in array) {
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top);
            if (lastStarView) {
                make.left.equalTo(lastStarView.mas_right).offset(1);
            } else make.left.equalTo(view.mas_right).offset(10);
            make.width.equalTo(@(10 * WIDTH / 375));
            make.height.equalTo(imageView.mas_width);
        }];
        lastStarView = imageView;
    }
}
- (void)array:(NSArray<UIImageView *> *)array
  makeRatings:(CGFloat)ratings{
    NSInteger wholeStar = (NSInteger)ratings;
    BOOL halfStar = (ratings - wholeStar) >= 0.5f;
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *starImageView = array[i];
        starImageView.image = [UIImage imageNamed:@"emptyStar"];
        if (i < wholeStar) {
            starImageView.image = [UIImage imageNamed:@"oneStar"];
        }
        if (i == wholeStar && halfStar) {
            starImageView.image = [UIImage imageNamed:@"halfStar"];
        }
    }
}



@end
